home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 8: LINUX Games / Linux Cubed Series 8 - LINUX Games.iso / games / x11 / networke / xfirepow.000 / xfirepow / xfirepower-0.84 / client / libsprite / event.c < prev    next >
C/C++ Source or Header  |  1995-05-23  |  7KB  |  313 lines

  1. #include "allincludes.h"
  2.  
  3. static struct window *
  4. findWindow(window)
  5.     Window  window;
  6. {
  7.     struct windowlist *entry;
  8.  
  9.     entry = hashtable[hash(window)];
  10.     while (entry != NULL) {
  11.     if (entry->window->window == window)
  12.         return (entry->window);
  13.     entry = entry->next;
  14.     }
  15.     return (NULL);
  16. }
  17.  
  18. void
  19. W_GetEvent(wevent)
  20.     W_Event *wevent;
  21. {
  22. /* blocks until an event is received [BDyess] */
  23.     XEvent  event;
  24.  
  25.     if (W_isEvent) {
  26.     *wevent = W_myevent;
  27.     W_isEvent = 0;
  28.     return;
  29.     }
  30.     XNextEvent(W_Display, &event);
  31.     XPutBackEvent(W_Display, &event);
  32.     W_SpNextEvent(wevent);
  33. }
  34.  
  35. int
  36. W_EventsPending()
  37. {
  38.     if (W_isEvent)
  39.     return (1);
  40.     while (XPending(W_Display) || buttonDown) {
  41.     if (W_SpNextEvent(&W_myevent)) {
  42.         W_isEvent = 1;
  43.         return (1);
  44.     }
  45.     }
  46.     return (0);
  47. }
  48.  
  49. void
  50. W_NextEvent(wevent)
  51.     W_Event *wevent;
  52. {
  53.     if (W_isEvent) {
  54.     *wevent = W_myevent;
  55.     W_isEvent = 0;
  56.     return;
  57.     }
  58.     while (W_SpNextEvent(wevent) == 0);
  59. }
  60.  
  61.  int
  62. W_SpNextEvent(wevent)
  63.     W_Event *wevent;
  64. {
  65.     XEvent  event;
  66.     XKeyEvent *key;
  67.     XButtonEvent *button;
  68.     XExposeEvent *expose;
  69.     XResizeRequestEvent *resize;
  70.     KeySym keysym;
  71.  
  72.     char    ch;
  73.     struct window *win;
  74. #ifdef CONTINUOUS_MOUSE
  75.     static W_Event buttonEvent;
  76.     static int delaytime, cupd = -1;
  77. #endif                /* CONTINUOUS_MOUSE */
  78.  
  79. #ifdef DEBUG
  80.     printf("Getting an event...\n");
  81. #endif
  82.     key = (XKeyEvent *) & event;
  83.     button = (XButtonEvent *) & event;
  84.     expose = (XExposeEvent *) & event;
  85.     resize = (XResizeRequestEvent *) & event;
  86.     for (;;) {
  87.     if (XPending(W_Display))
  88.         XNextEvent(W_Display, &event);
  89. #ifdef CONTINUOUS_MOUSE
  90.     else if (buttonDown) {
  91.         if (continuousMouse && allowContinuousMouse) {
  92.         if (cupd != udcounter) {
  93.             cupd = udcounter;
  94.             if (delaytime == 0) {
  95.             bcopy(&buttonEvent, wevent, sizeof(W_Event));
  96.             delaytime = clickDelay;
  97.             } else {
  98.             delaytime--;
  99.             wevent->type = -1;
  100.             }
  101.         } else
  102.             wevent->type = -1;
  103.         exitInputLoop = 1;
  104.         } else {
  105.         wevent->type = -1;
  106.         buttonDown = 0;
  107.         }
  108.         return (1);
  109.     }
  110. #endif
  111.     else
  112.         return (0);
  113.     /*
  114.        printf("read an event %d\n", event.type);
  115.     */
  116.     win = findWindow(key->window);
  117.     if (win == NULL)
  118.         return (0);
  119.     if ((event.type == KeyPress || event.type == ButtonPress) &&
  120.         win->type == WIN_MENU) {
  121.         if (key->y % (W_Textheight + MENU_PAD * 2 + MENU_BAR) >=
  122.         W_Textheight + MENU_PAD * 2)
  123.         return (0);
  124.         key->y = key->y / (W_Textheight + MENU_PAD * 2 + MENU_BAR);
  125.     }
  126.     switch ((int) event.type) {
  127.     case LeaveNotify:    /* for message window -- jfy */
  128.         return (0);
  129.         break;
  130.     case KeyPress:
  131.         if (key->state & ControlMask) {
  132.         controlkey = 1;
  133.         key->state &= ~ControlMask;
  134.         } else
  135.         controlkey = 0;
  136.         if (key->state & BillsScrewyAltMask) {
  137.         altkey = 1;
  138.         key->state &= ~BillsScrewyAltMask;
  139.         } else
  140.         altkey = 0;
  141.         if (XLookupString(key, &ch, 1, &keysym, NULL) > 0) {
  142.         wevent->type = W_EV_KEY;
  143.         wevent->Window = W_Window2Void(win);
  144.         wevent->x = key->x;
  145.         wevent->y = key->y;
  146.         if (controlkey)
  147.             wevent->key = (int) ch + 128;
  148.         else if (altkey)
  149.             wevent->key = (int) ch + 256;
  150.         else
  151.             wevent->key = ch;
  152.         return (1);
  153.         } else { /* trap arrow keys */
  154.         if(keysym == XK_Left || keysym == XK_Right ||
  155.            keysym == XK_Up || keysym == XK_Down) {
  156.             wevent->type = W_EV_KEY;
  157.             wevent->Window = W_Window2Void(win);
  158.             wevent->x = key->x;
  159.             wevent->y = key->y;
  160.             switch(keysym) {
  161.               case XK_Left:
  162.             wevent->key = (int)'b'+128; /* Left arrow --> ctrl-b */
  163.             break;
  164.               case XK_Right:
  165.             wevent->key = (int)'f'+128; /* Right arrow --> ctrl-f */
  166.             break;
  167.               case XK_Up:
  168.             wevent->key = (int)'p'+128;
  169.             break;
  170.               case XK_Down:
  171.             wevent->key = (int)'n'+128;
  172.             break;
  173.             }
  174.             return (1);
  175.         }
  176.         }
  177.         return (0);
  178.         break;
  179. #ifdef AUTOKEY
  180.     case KeyRelease:
  181.         if (XLookupString(key, &ch, 1, &keysym, NULL) > 0) {
  182.         wevent->type = W_EV_KEY_OFF;
  183.         wevent->Window = W_Window2Void(win);
  184.         wevent->x = key->x;
  185.         wevent->y = key->y;
  186.         wevent->key = ch;
  187.         return (1);
  188.         } else {
  189.         if(keysym == XK_Right || keysym == XK_Left ||
  190.            keysym == XK_Up || keysym == XK_Down) {
  191.             wevent->type = W_EV_KEY_OFF;
  192.             wevent->Window = W_Window2Void(win);
  193.             wevent->x = key->x;
  194.             wevent->y = key->y;
  195.             switch(keysym) {
  196.               case XK_Left:
  197.             wevent->key = (int)'b'+128; /* Left arrow --> ctrl-b */
  198.             break;
  199.               case XK_Right:
  200.             wevent->key = (int)'f'+128; /* Right arrow --> ctrl-f */
  201.             break;
  202.               case XK_Up:
  203.             wevent->key = (int)'p'+128;
  204.             break;
  205.               case XK_Down:
  206.             wevent->key = (int)'n'+128;
  207.             break;
  208.             }
  209.             return (1);
  210.         }
  211.         }        
  212.         return (0);
  213.         break;
  214. #endif                /* AUTOKEY */
  215.     case ButtonPress:
  216.         wevent->type = W_EV_BUTTON;
  217.         wevent->Window = W_Window2Void(win);
  218.         wevent->x = button->x;
  219.         wevent->y = button->y;
  220.         switch (button->button & 0xf) {
  221.         case Button3:
  222.         wevent->key = W_RBUTTON;
  223.         break;
  224.         case Button1:
  225.         wevent->key = W_LBUTTON;
  226.         break;
  227.         case Button2:
  228.         wevent->key = W_MBUTTON;
  229.         break;
  230.         }
  231.         if (key->state & ControlMask)
  232.         wevent->key += 6;
  233.         if (key->state & ShiftMask)
  234.         wevent->key += 3;
  235.         if (key->state & BillsScrewyAltMask)
  236.         wevent->key += 12;    /* alt */
  237. #ifdef CONTINUOUS_MOUSE
  238.         if (continuousMouse && allowContinuousMouse &&
  239.         (wevent->Window == w || wevent->Window == mapw) &&
  240.         /*
  241.            buttonRepeatMask allows only certain buttons to repeat
  242.            [BDyess]
  243.         */
  244.         (1 << (wevent->key) & buttonRepeatMask)) {
  245.         buttonDown = 1;
  246.         exitInputLoop = 1;
  247.         delaytime = clickDelay;
  248.         bcopy(wevent, &buttonEvent, sizeof(W_Event));
  249.         }
  250.         return (1);
  251.     case ButtonRelease:
  252.         /* bcopy(&buttonEvent,wevent,sizeof(W_Event)); */
  253.         wevent->type = -1;
  254.         buttonDown = 0;
  255.         return (1);
  256.     case MotionNotify:
  257.         /*
  258.            the !buttonDown ensures that if you press a button and then
  259.            press another, release just the second, and then move the
  260.            mouse that nothing happens.
  261.         */
  262.         if (!(continuousMouse && allowContinuousMouse) || !buttonDown) {
  263.         wevent->type = -1;
  264.         return (1);
  265.         }
  266.         wevent->type = W_EV_BUTTON;
  267.         wevent->Window = W_Window2Void(win);
  268.         wevent->x = button->x;
  269.         wevent->y = button->y;
  270.         wevent->key = buttonEvent.key;
  271.         bcopy(wevent, &buttonEvent, sizeof(W_Event));
  272.         if (cupd == udcounter)
  273.         wevent->type = -1;
  274.         else
  275.         cupd = udcounter;
  276.  
  277.         return (1);
  278. #else
  279.         return (1);
  280. #endif                /* CONTINUOUS_MOUSE */
  281.     case Expose:
  282.         if (expose->count != 0)
  283.         return (0);
  284.         if (win->type == WIN_SCROLL) {
  285.         redrawScrolling(win);
  286.         return (0);
  287.         }
  288.         if (win->type == WIN_MENU) {
  289.         redrawMenu(win);
  290.         return (0);
  291.         }
  292.         if (win->type == WIN_BORDER) {
  293.             redrawBorder(win);
  294.         return (0);
  295.         }
  296.         if (win == W_Void2Window(baseWin)) {
  297.             /* main window has a reverse border [BDyess] */
  298.         /*redrawReversedBorder(win);*/
  299.         }
  300.         wevent->type = W_EV_EXPOSE;
  301.         wevent->Window = W_Window2Void(win);
  302.         return (1);
  303.     case ResizeRequest:
  304.         resizeScrolling(win, resize->width, resize->height);
  305.         break;
  306.     default:
  307.         return (0);
  308.         break;
  309.     }
  310.     }
  311. }
  312.  
  313.